package com.scu.qingshi.facade.domain.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scu.qingshi.config.cummunity.CommunityFactory;
import com.scu.qingshi.domain.announcement.dto.InviteAnnouncementDTO;
import com.scu.qingshi.domain.announcement.enums.NoticeTypeEnum;
import com.scu.qingshi.facade.domain.AnnouncementQueryFacade;
import com.scu.qingshi.infrastructure.dao.po.Announcement;
import com.scu.qingshi.infrastructure.dao.service.AnnouncementService;
import com.scu.qingshi.infrastructure.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 通知查询接口
 * @author qingshi
 */
@RequestMapping("/noticeQuery")
@Api(tags = "通知查询")
@RestController
public class AnnouncementQueryFacadeImpl implements AnnouncementQueryFacade {

    @Autowired
    AnnouncementService announcementService;

    @GetMapping("/invite")
    @ApiOperation("查询来自俱乐部的邀请")
    @Override
    public List<InviteAnnouncementDTO> getInviteNotice(Integer userId) {
        try {
            QueryWrapper<Announcement> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",userId)
                    .eq("type", NoticeTypeEnum.INVITE_TO_JOIN.name());

            List<Announcement> announcements = announcementService.getBaseMapper().selectList(wrapper);
            return announcements.stream().map(announcement -> InviteAnnouncementDTO.builder()
                    .communityName(CommunityFactory.getCommunity(announcement.getOpenId()).getName())
                    .noticeId(announcement.getNoticeId())
                    .content(announcement.getContent())
                    .expireTime(DateUtil.format(announcement.getExpireTime()))
                    .isExpired(DateUtil.expirationDate(announcement.getExpireTime()))
                    .type(announcement.getType())
                    .openId(announcement.getOpenId())
                    .effective(announcement.getEffective())
                    .build()).collect(Collectors.toList());
        }catch (Exception e){
            e.printStackTrace();
        }
        return Collections.emptyList();
    }
}
